package java_collection.set;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class HashSetDemo {
	public static void main(String[] args) {
		
		Set<Integer> set = new HashSet<>();
		
		Integer[] list = {1,2,3,4,5,1};
		for (Integer s: list){
			set.add(s);
		}
		
		add(set);
		toStringDemo(set);
		removeDemo(set);
		forEachLoopDemo(set);
		iteratorDemo(set);
		containsDemo(set);
		addAllDemo(set);
		removeAllDemo(set);
		clearDemo(set);
	}

	
	private static void add(Set<Integer> set){
		System.out.println("\n-------------- add() Demo-----------------");
		System.out.println("Set entries before addition: " + set);
		System.out.println("Set size: " + set.size());
		
		Integer[] list = {6,7,8,9,10,9,10};
		
		System.out.println("Adding values {6,7,8,9,10,9,10}");
		// Add values in Set
		for (Integer s: list){
			System.out.println(set.add(s)? s + " added" : s + " not added");
		}
		
		System.out.println("Set entries after addition: " + set);
		System.out.println("Set size: " + set.size());
	}
	
	private static void toStringDemo(Set<Integer> set){
		System.out.println("-------------- toString Demo-----------------");
		
		// below two lines will call toString()
		System.out.println(set.toString());
		System.out.println(set);
	}
	
	private static void removeDemo(Set<Integer> set){
		System.out.println("\n-------------- remove() Demo-----------------");
		System.out.println("Set entries: " + set);
		
		System.out.println("Removing 1 and 20");
		Integer temp = 1;
		System.out.println(set.remove(temp)? temp + " removed" : temp + " not removed");
		
		temp = 20;
		System.out.println(set.remove(temp)? temp + " removed" : temp + " not removed");
		
		// add back removed entry
		temp = 1;
		System.out.println(set.add(temp)? temp + " added" : temp + " not added");
	}
	
	private static void forEachLoopDemo(Set<Integer> set){
		System.out.println("\n-------------- Print each element using for each loop ----------------");
		// for each loop
		for (Integer s: set){
			System.out.print(s + " ");
		}
		System.out.println("\n");
	}
	
	private static void iteratorDemo(Set<Integer> set){
		// Using Iterator
		System.out.println("\n ---------- Print each element using Iterator -----------");
		Iterator<Integer> itr = set.iterator();
		
		while (itr.hasNext()){
			System.out.print(itr.next() + " ");
			
		}
		System.out.println("\n");
	}
	private static void containsDemo(Set<Integer> set){
		// check if value exist in set
		System.out.println("\n-------------- contains() Demo-----------------");
		System.out.println("Set entries: " + set);
		System.out.println("Check if 2 and 20 exists in set");
		
		Integer temp = 2;
		System.out.println(set.contains(temp)? temp + " exists" : temp + " not exist");
		temp = 20;
		System.out.println(set.contains(temp)? temp + " exists" : temp + " not exist");
	}
	
	private static void addAllDemo(Set<Integer> set){
		System.out.println("\n-------------- addAll() Demo-----------------");
		// addAll() - Adds all of the elements in the specified collection to this set if they're not already present
		Collection<Integer> subList = new ArrayList<>();
		
		subList.add(9);
		subList.add(10);
		subList.add(11);
		subList.add(12);
		subList.add(13);
		subList.add(14);
		
		System.out.println("Set entries before addition: " + set);
		System.out.println("Adding values :" + subList);
		
		set.addAll(subList);
		System.out.println("Set entries after addition: " + set);
	}
	
	private static void removeAllDemo(Set<Integer> set) {
		// removing 1,2,3 from set
		System.out.println("Set entries before removal: " + set);
		System.out.println("removing 1,2,3 from set");
		set.removeAll(Arrays.asList(1,2,3));
		System.out.println("Set entries after removal: " + set);
	}
	
	private static void clearDemo(Set<Integer> set){
		System.out.println("\n-------------- clear() Demo-----------------");
		System.out.println("Set size: " + set.size());
		System.out.println("isEmpty : " + set.isEmpty());
		
		System.out.println("removing all entries from set");
		set.clear();
		
		System.out.println("Set size: " + set.size());
		System.out.println("isEmpty : " + set.isEmpty());
	}
}
